/**
 * KopsoxFramework
 * Copyright (C) 2010 Free Software Foundation, Inc. <http://fsf.org/>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kopsox.util.data;

import java.io.Serializable;

import org.kopsox.data.ValueDescriptor;
import org.kopsox.data.impl.Polymorph;
import org.kopsox.data.impl.ValueDescriptorImpl;
import org.kopsox.general.range.Range;
import org.kopsox.general.range.impl.IntegerRange;

/**
 * This factory provides Methods for creating Polymorphs.<br>
 * 
 * @author Konrad Renner - 06.11.2010
 *
 */
public final class PolymorphFactory {

	/**
	 * Creates an Polymorph which has an Readonly-Validator
	 * 
	 * @param <T>
	 * @param value
	 * @param name
	 * @param namespace
	 * @return Polymorph<T>
	 */
	public static final <T extends Serializable> Polymorph<T> createImmutablePolymorph(T value,String name,String namespace){
		ValueDescriptor desc = new ValueDescriptorImpl.ValueDescriptorBuilder(name).namespace(namespace).isAutoValidationEnabled(true).isChangeable(false).build();
		return new Polymorph<T>(value,desc);
	}
	
	/**
	 * Creates an Polymorph which has an NotNull-Validator
	 * 
	 * @param <T>
	 * @param value
	 * @param name
	 * @param namespace
	 * @return Polymorph<T>
	 */
	public static final <T extends Serializable> Polymorph<T> createNotNullablePolymorph(T value,String name,String namespace){
		ValueDescriptor desc = new ValueDescriptorImpl.ValueDescriptorBuilder(name).namespace(namespace).isAutoValidationEnabled(true).isNullable(false).build();
		return new Polymorph<T>(value,desc);
	}
	
	/**
	 * Creates an Polymorph which has an ValueRange-Validator
	 * 
	 * @param <T>
	 * @param value
	 * @param range 
	 * @param name
	 * @param namespace
	 * @return Polymorph<T>
	 */
	public static final <T extends Serializable> Polymorph<T> createPolymorphWithValueRange(T value,Range range,String name,String namespace){
		ValueDescriptor desc = new ValueDescriptorImpl.ValueDescriptorBuilder(name).namespace(namespace).isAutoValidationEnabled(true).rangeOfValue(range).build();
		return new Polymorph<T>(value,desc);
	}
	
	/**
	 * Creates an Polymorph which has an DigitsRange-Validator
	 * 
	 * @param <T>
	 * @param value
	 * @param digits 
	 * @param name
	 * @param namespace
	 * @return Polymorph<T>
	 */
	public static final <T extends Serializable> Polymorph<T> createPolymorphWithDigitsRange(T value,IntegerRange digits,String name,String namespace){
		ValueDescriptor desc = new ValueDescriptorImpl.ValueDescriptorBuilder(name).namespace(namespace).isAutoValidationEnabled(true).rangeOfDigits(digits).build();
		return new Polymorph<T>(value,desc);
	}
	
	/**
	 * Creates an Polymorph which has an ValueRange-Validator and DigitsRange-Validator
	 * 
	 * @param <T>
	 * @param value
	 * @param range 
	 * @param digits 
	 * @param name
	 * @param namespace
	 * @return Polymorph<T>
	 */
	public static final <T extends Serializable> Polymorph<T> createPolymorphWithValueRangeAndDigitsRange(T value,Range range,IntegerRange digits,String name,String namespace){
		ValueDescriptor desc = new ValueDescriptorImpl.ValueDescriptorBuilder(name).namespace(namespace).isAutoValidationEnabled(true).rangeOfValue(range).rangeOfDigits(digits).build();
		return new Polymorph<T>(value,desc);
	}
	
	/**
	 * Creates an Polymorph which has an NotNull-Validator, ValueRange-Validator and DigitsRange-Validator<br>
	 * NOTE: If the Range-Param is null, there will no Range-Validator being mapped<br>
	 * NOTE: If the Digits-Param is null, there will no DigitsRange-Validator being mapped<br>
	 * 
	 * @param <T>
	 * @param value
	 * @param range 
	 * @param digits 
	 * @param name
	 * @param namespace
	 * @return Polymorph<T>
	 */
	public static final <T extends Serializable> Polymorph<T> createNotNullablePolymorphWithValueRangeAndDigitsRange(T value,Range range,IntegerRange digits,String name,String namespace){
		ValueDescriptor desc = new ValueDescriptorImpl.ValueDescriptorBuilder(name).namespace(namespace).isAutoValidationEnabled(true).isNullable(false).rangeOfValue(range).rangeOfDigits(digits).build();
		return new Polymorph<T>(value,desc);
	}
	
	/**
	 * Creates an Polymorph where autoValidation is disabled
	 * 
	 * @param <T>
	 * @param value
	 * @param name
	 * @param namespace
	 * @return Polymorph<T>
	 */
	public static final <T extends Serializable> Polymorph<T> createPolymorphWithoutValidation(T value,String name,String namespace){
		ValueDescriptor desc = new ValueDescriptorImpl.ValueDescriptorBuilder(name).namespace(namespace).isAutoValidationEnabled(false).build();
		return new Polymorph<T>(value,desc);
	}
}
